'----------------------------------------------
' Name: Ali Karbassi & Ben Sattler
' Date: February 15, 2007
' Description: Program 2
'              Form frmHouse
'----------------------------------------------

Imports UWPCS3340

Public Class frmHouse

   ''' <summary>
   ''' Clears the form. This will clear all previous values on the form and the user can enter values for a new
   ''' house. Button cmdModify will be disabled, while all other buttons will be enabled.
   ''' </summary>
   ''' <remarks></remarks>
   Public Sub clearForm()

      txtID.Clear()
      txtID.ReadOnly = False
      txtPrice.Clear()
      cboHouse.Enabled = True
      cboHouse.SelectedIndex = -1
      rdoRooms2.Checked = False
      rdoRooms3.Checked = False
      rdoRooms4.Checked = False
      rdoGarage1.Checked = False
      rdoGarage2.Checked = False
      rdoGarage3.Checked = False

      ' Disable Modify Button
      cmdModify.Enabled = False

      ' Enable the rest of the buttons.
      cmdClear.Enabled = True
      cmdSave.Enabled = True
      cmdList.Enabled = True
      cmdExit.Enabled = True

   End Sub

   ''' <summary>
   ''' Calls clearForm which will clear the form.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
   cmdClear.Click

      clearForm()

   End Sub

   ''' <summary>
   ''' Clicking cmdSave will save a new house to the list of houses. Only when the type is "Madison," the 
   ''' click event procedure should check the ID (not any other values) in order to call the constructor with
   ''' a parameter. The client code does not validate any other values for the house, but must catch any error
   ''' raised by the class code. After a new house is saved successfully, button cmdSave will be disabled, 
   ''' while other buttons will be enabled.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

      If cboHouse.SelectedIndex = -1 Then
         MsgBox("Select a type first!", MsgBoxStyle.Information)
      Else

         Dim b As Boolean = False

         For i As Integer = 0 To houseList.Count - 1
            If txtID.Text = CType(houseList.Item(i), House).ID Then
               b = True
            End If
         Next

         If b = True Then
            MsgBox("This ID already exists!", MsgBoxStyle.Information)
         Else

            Try

               Dim h As House = Nothing

               Select Case cboHouse.Text
                  Case "Madison" : h = New Madison(txtID.Text)
                  Case "Platteville" : h = New Platteville
                  Case "Chicago" : h = New Chicago
               End Select

               If Not h.Type = "Madison" Then
                  h.ID = txtID.Text
               End If

               setInfo(h)

               houseList.Add(h)

               frmList.populateListBox()

               ' Disable things
               txtID.ReadOnly = True
               txtID.BackColor = Color.White
               cmdModify.Enabled = True
               cboHouse.Enabled = False

            Catch ex As Exception

               Dim s As String = "Error message: " & ex.Message & vbCrLf & "Error Source: " & ex.Source & vbCrLf & "Stack Trace" & ex.StackTrace

               MsgBox(s)
            End Try

         End If

      End If

   End Sub

   ''' <summary>
   ''' Clicking cmdExit will terminate the project.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click

      Application.Exit()

   End Sub

   ''' <summary>
   ''' Clicking cmdModify will save the changes made to the house displayed on the form. The client code
   ''' should just call the class methods without checking the input and catch any possible error. When the
   ''' number of rooms and/or the number of garages of a house has been modified, the price will be modified
   ''' accordingly and the new price will be displayed on the form.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
   cmdModify.Click

      Try

         Dim h As House = Nothing

         For i As Integer = 0 To houseList.Count - 1 Step 1
            If CType(houseList.Item(i), House).ID = txtID.Text Then
               h = CType(houseList.Item(i), House)
            End If
         Next

         setInfo(h)

         updateForm()
         frmList.populateListBox()

      Catch ex As Exception

         Dim s As String = "Error message: " & ex.Message & vbCrLf & "Error Source: " & ex.Source & vbCrLf & "Stack Trace" & ex.StackTrace
         MsgBox(s)

      End Try

   End Sub

   ''' <summary>
   ''' Clicking cmdList will display form frmList in the center of the screen.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdList.Click

      Me.Hide()
      frmList.Show()

   End Sub

   ''' <summary>
   ''' Sets the form to display the information of the given ID. This does not check to see if ID exists.
   ''' </summary>
   ''' <param name="id"></param>
   ''' <remarks></remarks>
   Public Sub setHouseAs(ByVal id As String)

      txtID.Text = id
      Dim h As House = Nothing
      For i As Integer = 0 To houseList.Count - 1 Step 1
         If CType(houseList.Item(i), House).ID = id Then
            h = CType(houseList.Item(i), House)
         End If
      Next
      cboHouse.Text = h.Type

      If h.Rooms = 2 Then
         rdoRooms2.Checked = True
      ElseIf h.Rooms = 3 Then
         rdoRooms3.Checked = True
      ElseIf h.Rooms = 4 Then
         rdoRooms4.Checked = True
      End If

      If h.Garages = 1 Then
         rdoGarage1.Checked = True
      ElseIf h.Garages = 2 Then
         rdoGarage2.Checked = True
      ElseIf h.Garages = 3 Then
         rdoGarage3.Checked = True
      End If

      txtPrice.Text = Format(h.Price, "currency")

   End Sub

   ''' <summary>
   ''' Sets the info of the house.
   ''' </summary>
   ''' <param name="h">House to be set</param>
   ''' <remarks></remarks>
   Public Sub setInfo(ByVal h As House)

      If rdoRooms2.Checked Then
         h.Rooms = 2
      ElseIf rdoRooms3.Checked Then
         h.Rooms = 3
      ElseIf rdoRooms4.Checked Then
         h.Rooms = 4
      Else
         h.Rooms = -1
      End If

      h.setRooms()

      If rdoGarage1.Checked Then
         h.Garages = 1
      ElseIf rdoGarage2.Checked Then
         h.Garages = 2
      ElseIf rdoGarage3.Checked Then
         h.Garages = 3
      Else
         h.Garages = -1
      End If

      h.setGarages()

      h.setPrice()

      txtPrice.Text = Format(h.Price.ToString, "currency")

   End Sub

   ''' <summary>
   ''' Updates the form.
   ''' </summary>
   ''' <remarks></remarks>
   Public Sub updateForm()

      frmList.txtHouseCount.Text = houseList.Count

   End Sub

End Class
